<?php 
/**
 * Controller is the base class for classes containing controller logic.
 *
 * @copyright Copyright (c) 2015 Swolf Software.
 * @author Liuxiao<seanliu707@gmail.com>
 */
class Controller
{
	public function __construct()
	{
	}
	
	/**
	 * Populates the model with the data from end user.
	 */
	protected function getModel($model)
	{
		$model = ucfirst($model);
		$model_file = Application::getConfig('modelPath') . $model . '.php';
		if (file_exists($model_file)) {
		    //if seted up the db type and load the corresponding ancestor model class
		    if (Application::getConfig('db_type') != false) {
		        Swolf::load('model/' . Application::getConfig('db_type') . 'Model');
		    } else {
		        //default load the Mysql model class
		        Swolf::load('model/MysqlModel');
		    }
		    
			Swolf::load('base.Model');
			require_once $model_file;
			return new $model;
		}
	}
	
	/**
	 * Returns the view object that can be used to render views or view files.
	 * this view object to implement the actual view rendering.
	 * @return the view object that can be used to render views or view files.
	 */
	protected function getView($_cache_time = 0)
	{
		//cache time 0 secend default.
		$_view = Swolf::createObject('view.ViewFactory')->create('html', $_cache_time);
		$_view->_tpl_dir	= Application::getConfig('viewPath');
		$conf = Application::getConfig();
		$_view->_cache_dir	= $conf['runtime']['cache'];
		$_view->assoc('Config', $conf);
		return $_view;
	}
	
	/**
	 * Redirect function.
	 *     @see base/UrlManager 
	 *     
	 * @param string $page
	 * @param array $args
	 */
	protected function redirect($page = null, $args = null)
	{
	    $uri = Swolf::createObject('base/UrlManager');
	    $uri->redirect($page, $args);
	}
}